package manager.customer.service;
import common.NumberUtils;
import common.SnowflakeIdWorker;
import manager.customer.dao.PositionMapper;
import manager.customer.dao.ReceiverMapper;
import manager.customer.dao.CustomerMapper;
import manager.customer.vo.PositionVO;
import manager.customer.vo.ReceiverVO;
import manager.customer.vo.Customer;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private ReceiverMapper receiverMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private AmqpTemplate amqpTemplate;
    //要检验的数据内容，type为校验种类，2，手机，1,代表用户名
    //没有查到返回true，查到返回false
    public Boolean checkData(String data, Integer type) {
        Customer customer = new Customer();
        if (1 == type) {
            customer.setUsername(data);
        } else if (2 == type) {
            customer.setPhone(data);
        }
        return this.customerMapper.selectCount(customer) == 0;
    }

    //要发送短信
    public Boolean sendSms(String phone) {
        //生成一个随机的验证码
        amqpAdmin.declareExchange(new DirectExchange("crmshop.sms.exchange"));
        amqpAdmin.declareQueue(new Queue("crmshop.sms.queen",true));
        amqpAdmin.declareBinding(new Binding("crmshop.sms.queen", Binding.DestinationType.QUEUE,"crmshop.sms.exchange","sms.verify.code",null));
        String code = NumberUtils.generateCode(6);
        Map<String, String> msg = new HashMap<>();
        msg.put("code", code);
        msg.put("phone", phone);
        amqpTemplate.convertAndSend("crmshop.sms.exchange", "sms.verify.code", msg);
        //把数据存入redis并且5min有效
        this.redisTemplate.opsForValue().set("crmshop." + phone, code, 5, TimeUnit.MINUTES);
        return true;
    }
    /**
     * 首先根据phone去redis中取值，取完之后校验，校验成功继续
     *
     * @param customer
     * @return
     */
    public Boolean register(Customer customer) {
        try {
            //所有的校验都完成之后可以做数据保存
            String salt=BCrypt.gensalt();
            String currentPass=BCrypt.hashpw(customer.getPassword(),salt);
            customer.setId(SnowflakeIdWorker.nextId());
            customer.setPassword(currentPass);
            customer.setSalt(salt);
            customer.setCreated(new Date());
            this.customerMapper.insertSelective(customer);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 根据条件获取地名信息并且存入redies中
     * @return
     */
    public List<PositionVO> getPositionByCondition(PositionVO positionVO) {
        //如果没有
        List<PositionVO> positionVOList =positionMapper.select(positionVO);
        return positionVOList;
    }

    public List<Customer> listUser(Customer customer) {
        List<Customer> storeCustomer =this.customerMapper.select(customer);
        ReceiverVO receiverVO=new ReceiverVO();
        receiverVO.setUserId(String.valueOf(storeCustomer.get(0).getId()));
        List<ReceiverVO> receiverList=this.receiverMapper.select(receiverVO);
        storeCustomer.get(0).setReceiverVOList(receiverList);
        return storeCustomer;
    }
    //添加用户地址信息
    public Boolean addReceiver(ReceiverVO receiverVO) {
         String receiverId=String.valueOf(SnowflakeIdWorker.nextId());
         receiverVO.setId(receiverId);
         receiverMapper.insertSelective(receiverVO);
         return true;
    }
    //根据用户id查询收货地址
    public List<ReceiverVO> getReceiverByCondition(ReceiverVO receiverVO) {
        return receiverMapper.selectByExample(receiverVO);
    }
   //更新数据
    public void updateReceiver(ReceiverVO receiverVO) {
        receiverMapper.updateByPrimaryKeySelective(receiverVO);
    }
    //删除收货地址
    public void deleteReceiver(ReceiverVO receiverVO) {
        receiverMapper.delete(receiverVO);
    }
}
